package com.ylbh.modules.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.business.dto.PrefabricatedGoodsDTO;
import com.ylbh.common.pojo.businessgoods.dto.BusinessGoods1DTO;
import com.ylbh.common.pojo.businessgoods.dto.PrefabricatedGoodsDetailsDTO;
import com.ylbh.common.pojo.businessgoods.dto.UpdateBusinessGoodsDTO;
import com.ylbh.common.pojo.businessgoods.vo.BusinessGoodsDetailsVO;
import com.ylbh.common.pojo.businessgoods.vo.BusinessGoodsListVO;
import com.ylbh.common.pojo.businessgoods.vo.PrefabricatedGoodsClassVO;
import com.ylbh.common.pojo.businessgoods.vo.PrefabricatedGoodsDetailsVO;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.modules.business.entity.ShoppingBusinessGoods;
import com.ylbh.modules.business.mapper.ShoppingBusinessGoodsMapper;
import com.ylbh.modules.business.service.ShoppingBusinessGoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.utils.PageUtils;

@Service
public class ShoppingBusinessGoodsServiceImpl extends ServiceImpl<ShoppingBusinessGoodsMapper, ShoppingBusinessGoods> implements ShoppingBusinessGoodsService {

    private final String shoppingMainImage = "http://image.yilianbaihui.cn/consumer/";

    @Autowired
    private ShoppingBusinessGoodsMapper shoppingBusinessGoodsMapper;

    /**
     * 联盟店商品列表
     * @param businessGoodsListDTO
     * @return
     */
    @Override
    public PageUtils queryBusinessGoodsList(BusinessGoods1DTO businessGoodsListDTO) {
        Page<Object> page = new Page<>(businessGoodsListDTO.getPage(), businessGoodsListDTO.getLimit());
        List<BusinessGoodsListVO> goodsList = shoppingBusinessGoodsMapper.selectBusinessGoodsList(businessGoodsListDTO, page);
        for (BusinessGoodsListVO b : goodsList) {
            if (!StringUtils.isEmpty(b.getGoodsImg())) {
                b.setMainImg(shoppingMainImage + "goods/" + b.getGoodsImg());
            }
        }
        Page<BusinessGoodsListVO> page1 = new Page<>();
        page1.setTotal(page.getTotal());
        page1.setRecords(goodsList);
        return new PageUtils(page1);
    }

    /**
     * 未审核联盟店商品
     * @return
     */
    @Override
    public int queryCount() {
        return shoppingBusinessGoodsMapper.selectCount();
    }

    /**
     * 对联盟店商品进行上架操作
     * @param goodsId
     */
    @Override
    public void modifyIsSale(Long goodsId) {
        shoppingBusinessGoodsMapper.updateIsSale(goodsId);
    }

    /**
     * 对联盟店商品进行下架操作
     * @param goodsId
     */
    @Override
    public void modifyNoIsSale(Long goodsId) {
        shoppingBusinessGoodsMapper.updateNoIsSale(goodsId);
    }

    /**
     * 根据id查看联盟店的商品及商家信息
     * @param goodsId
     * @return
     */
    @Override
    public BusinessGoodsDetailsVO getGoodsById(Long goodsId) {
        BusinessGoodsDetailsVO byId = shoppingBusinessGoodsMapper.selectGoodsById(goodsId);
        if (null != byId)
            if (!StringUtils.isEmpty(byId.getGoodsDescImg())) {
                List<String> list = Arrays.asList(byId.getGoodsDescImg().split(","));
                byId.setDescImg(list);
            }
        return byId;
    }

    /**
     * 对联盟店商品进行通过操作
     * @param goodsId
     */
    @Override
    public void modifyAdopt(Long goodsId) {
        shoppingBusinessGoodsMapper.updateAdopt(goodsId);
        // 查询商品的条形码，如果商品库不存在，则复制到商品库中
        int barCode = shoppingBusinessGoodsMapper.selectBarCodeById(goodsId.toString());
        if (barCode == 0) {
            ShoppingBusinessGoods entity = shoppingBusinessGoodsMapper.selectIsCommodityBankById(goodsId.toString());
            if (null != entity) {
                // 添加到商品库中
                PrefabricatedGoodsDetailsDTO dto = new PrefabricatedGoodsDetailsDTO();
                dto.setGoodsId(Long.parseLong(new IdWorker().nextId()));
                dto.setGoodsDescImgs(entity.getGoodsdescimgs());
                dto.setDate(new Date());
                dto.setGoodsImg(entity.getGoodsimg());
                dto.setGoodsName(entity.getGoodsname());
                dto.setGoodsSn(entity.getGoodssn());
                dto.setUserId(entity.getClassId());
                dto.setVipPrice(entity.getVipprice());
                dto.setMarketPrice(entity.getMarketprice());
                dto.setTypeId(entity.getTypeid());
                dto.setGoodsDesc(entity.getGoodsdesc());
                dto.setDistributionTypes(entity.getDistributiontypes());
                dto.setIsRecomGoods(Integer.parseInt(entity.getIsrecomgoods()));
                dto.setIsNewGoods(Integer.parseInt(entity.getIsnewgoods()));
                dto.setIsCommission(Integer.parseInt(entity.getIscommission()));
                dto.setCommission(entity.getCommission());
                dto.setInitialInventory(entity.getInitialinventory());
                dto.setDeliverMoney(entity.getDelivermoney());
                dto.setGoodsDetails(entity.getGoodsdesc());
                dto.setBarCode(entity.getBarCode());
                shoppingBusinessGoodsMapper.insertPrefabricatedGoods(dto);
            }
        }
    }

    /**
     * 对联盟店商品进行不通过操作
     * @param goodsId
     */
    @Override
    public void modifyNoAdopt(Long goodsId, String statusRemarks) {
        shoppingBusinessGoodsMapper.updateNoAdopt(goodsId, statusRemarks);
    }

    /**
     * 对联盟店的商品批量审核通过操作
     * @param ids
     */
    @Override
    public void modifyBatchPassByIds(List<String> ids) {
        shoppingBusinessGoodsMapper.updateBatchPassByIds(ids);
    }

    /**
     * 对联盟店的商品批量审核不通过操作
     * @param ids
     */
    @Override
    public void modifyBatchNoPassByIds(List<String> ids) {
        shoppingBusinessGoodsMapper.updateBatchNoPassByIds(ids);
    }

    /**
     * 对联盟店的商品进行批量审核操作
     * @param ids
     * @param batch
     */
    @Override
    public void modifyBatchIds(List<String> ids, Integer batch) {
        shoppingBusinessGoodsMapper.updateBatchByIds(ids, batch);
        if (batch == 1) {
            // 查询商品的条形码，如果商品库不存在，则复制到商品库中
            for (String id : ids) {
                int barCode = shoppingBusinessGoodsMapper.selectBarCodeById(id);
                if (barCode == 0) {
                    ShoppingBusinessGoods entity = shoppingBusinessGoodsMapper.selectIsCommodityBankById(id);
                    if (null != entity) {
                        // 添加到商品库中
                        PrefabricatedGoodsDetailsDTO dto = new PrefabricatedGoodsDetailsDTO();
                        dto.setGoodsDescImgs(entity.getGoodsdescimgs());
                        dto.setGoodsId(Long.parseLong(new IdWorker().nextId()));
                        dto.setDate(new Date());
                        dto.setGoodsName(entity.getGoodsname());
                        dto.setGoodsImg(entity.getGoodsimg());
                        dto.setGoodsSn(entity.getGoodssn());
                        dto.setUserId(entity.getClassId());
                        dto.setMarketPrice(entity.getMarketprice());
                        dto.setVipPrice(entity.getVipprice());
                        dto.setTypeId(entity.getTypeid());
                        dto.setGoodsDesc(entity.getGoodsdesc());
                        dto.setDistributionTypes(entity.getDistributiontypes());
                        dto.setIsNewGoods(Integer.parseInt(entity.getIsnewgoods()));
                        dto.setIsRecomGoods(Integer.parseInt(entity.getIsrecomgoods()));
                        dto.setIsCommission(Integer.parseInt(entity.getIscommission()));
                        dto.setCommission(entity.getCommission());
                        dto.setDeliverMoney(entity.getDelivermoney());
                        dto.setInitialInventory(entity.getInitialinventory());
                        dto.setGoodsDetails(entity.getGoodsdesc());
                        dto.setBarCode(entity.getBarCode());
                        shoppingBusinessGoodsMapper.insertPrefabricatedGoods(dto);
                    }
                }
            }
        }
    }

    /**
     * 对联盟店的商品详情修改
     * @param updateBusinessGoodsDTO
     */
    @Override
    public void modifyGoodsById(UpdateBusinessGoodsDTO updateBusinessGoodsDTO) {
        if (updateBusinessGoodsDTO.getDescImg().size() > 0) {
            String s = String.join(",", updateBusinessGoodsDTO.getDescImg());
            updateBusinessGoodsDTO.setDescImgs(s);
        }else {
            updateBusinessGoodsDTO.setDescImgs("");
        }
        shoppingBusinessGoodsMapper.updateGoodsById(updateBusinessGoodsDTO);
    }

    /**
     * 预制商品列表
     * @param prefabricatedGoodsDTO
     * @return
     */
    @Override
    public PageUtils queryPrefabricatedGoods(PrefabricatedGoodsDTO prefabricatedGoodsDTO) {
        Page<Object> page = new Page<>(prefabricatedGoodsDTO.getPage(), prefabricatedGoodsDTO.getLimit());
        List<BusinessGoodsListVO> list = shoppingBusinessGoodsMapper.selectPrefabricatedGoods(prefabricatedGoodsDTO, page);
        return new PageUtils(new Page<BusinessGoodsListVO>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 预制商品详情
     * @param goodsId
     * @return
     */
    @Override
    public PrefabricatedGoodsDetailsVO getPrefabricatedGoodsById(Long goodsId) {
        PrefabricatedGoodsDetailsVO goods = shoppingBusinessGoodsMapper.selectPrefabricatedGoodsById(goodsId);
        if (StringUtils.isNotBlank(goods.getGoodsDescImgs())) {
            List<String> list = Arrays.asList(goods.getGoodsDescImgs().split(","));
            goods.setDescImg(list);
        }
        return goods;
    }

    /**
     * 修改预制商品
     * @param prefabricatedGoodsDetailsDTO
     */
    @Override
    public void modifyPrefabricatedGoodsById(PrefabricatedGoodsDetailsDTO prefabricatedGoodsDetailsDTO) {
        if (prefabricatedGoodsDetailsDTO.getDescImg().size() > 0) {
            String s = String.join(",", prefabricatedGoodsDetailsDTO.getDescImg());
            prefabricatedGoodsDetailsDTO.setGoodsDescImgs(s);
        }else {
            prefabricatedGoodsDetailsDTO.setGoodsDescImgs("");
        }
        shoppingBusinessGoodsMapper.updatePrefabricatedGoodsById(prefabricatedGoodsDetailsDTO);
    }

    /**
     * 新增预制商品
     * @param prefabricatedGoodsDetailsDTO
     */
    @Override
    public void addPrefabricatedGoods(PrefabricatedGoodsDetailsDTO prefabricatedGoodsDetailsDTO) {
        if (prefabricatedGoodsDetailsDTO.getDescImg().size() > 0) {
            String s = String.join(",", prefabricatedGoodsDetailsDTO.getDescImg());
            prefabricatedGoodsDetailsDTO.setGoodsDescImgs(s);
        }else {
            prefabricatedGoodsDetailsDTO.setGoodsDescImgs("");
        }
        prefabricatedGoodsDetailsDTO.setGoodsId(Long.parseLong(new IdWorker().nextId()));
        prefabricatedGoodsDetailsDTO.setDate(new Date());
        shoppingBusinessGoodsMapper.insertPrefabricatedGoods(prefabricatedGoodsDetailsDTO);
    }

    /**
     * 批量修改预制商品
     * @param batch
     * @param userClass
     * @param ids
     */
    @Override
    public void modifyBatchPrefabricatedGoodsById(String batch, Integer userClass, String[] ids) {
        shoppingBusinessGoodsMapper.updateBatchPrefabricatedGoodsById(batch, userClass,Arrays.asList(ids));
    }

    /**
     * 上下架预制商品
     * @param isSale
     * @param goodsId
     */
    @Override
    public void modifyIsSaleByGoodsId(String isSale, Long goodsId) {
        shoppingBusinessGoodsMapper.updateIsSaleByGoodsId(isSale, goodsId);
    }

    /**
     * 商品重新审核
     * @param goodsId
     * @param status
     * @param statusRemarks
     */
    @Override
    public void modifyGoodsAdopt(String goodsId, Integer status, String statusRemarks) {
        shoppingBusinessGoodsMapper.updateGoodsAdopt(goodsId, status, statusRemarks);
    }

    /**
     * 预制商品分类
     * @return
     */
    @Override
    public List<PrefabricatedGoodsClassVO> getPrefabricatedGoodsClass() {
        return shoppingBusinessGoodsMapper.selectPrefabricatedGoodsClass();
    }

}
